package com.veo.controller;

import com.veo.entity.Order;
import com.veo.mapper.OrderMapper;
import com.veo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/adminOrder")
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 去订单管理页面
     * @return
     */
    @RequestMapping("/toOrder")
    public String toOrder(){
        return "/admin/orderManage";
    }


    /**
     * 查询所有订单
     * @return
     */
    @RequestMapping("/getAllOrder")
    @ResponseBody
    public List<Order> getAllOrder(){
        List<Order> allOrder = orderService.findAllOrder();
        return orderService.findAllOrder();
    }

    /**
     * 修改订单的发货状态
     * @param id
     * @return
     */
    @RequestMapping("/updateDeliveryStatus")
    @ResponseBody
    public Map<String,Object> updateDeliveryStatus(Integer id){
        Order order = orderMapper.selectById(id);
        order.setDeliveryStatus("1");
        int i = orderMapper.updateById(order);
        return returnMsg(i);
    }

    /**
     * 根据id查订单
     */
    @GetMapping("/findOrderById")
    public String findOrderById(Integer id, Model model){
        Order order = orderService.findOrderById(id);
        model.addAttribute("order",order);
        return "admin/orderDetails";
    }

    public Map<String, Object> returnMsg(Integer i){
        Map<String, Object> map = new HashMap<>();
        if (i > 0){
            map.put("msg", "ok");
            return map;
        }else {
            map.put("msg","no");
            return map;
        }
    }
}
